package com.billdowney.service.impl.system.notice;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.billdowney.dao.system.notice.SystemNoticeAddresseeDao;
import com.billdowney.dao.system.notice.SystemNoticeAttachmentDao;
import com.billdowney.dao.system.notice.SystemNoticeDao;
import com.billdowney.entity.file.FileInfo;
import com.billdowney.entity.system.notice.SystemNotice;
import com.billdowney.entity.system.notice.SystemNoticeAddressee;
import com.billdowney.entity.system.notice.SystemNoticeAttachment;
import com.billdowney.exception.BizException;
import com.billdowney.service.impl.base.BaseServiceImpl;
import com.billdowney.service.interfaces.system.notice.SystemNoticeService;
import com.billdowney.util.CommonUtil;

/**
 * 系统通知接口实现
 * 
 * @author 超级小富翁 - BillDowney
 * @date 2019年3月28日 上午9:37:34
 */
@Service
public class SystemNoticeServiceImpl extends BaseServiceImpl<SystemNoticeDao, SystemNotice, String>
		implements SystemNoticeService {

	// 系统通知收件人
	@Autowired
	private SystemNoticeAddresseeDao systemNoticeAddresseeDao;
	// 系统通知附件
	@Autowired
	private SystemNoticeAttachmentDao systemNoticeAttachmentDao;

	@Resource(type = SystemNoticeDao.class)
	@Override
	public void setDao(SystemNoticeDao dao) {
		super.dao = dao;
	}

	@Override
	public SystemNotice save(SystemNotice systemNotice) {
		this.checkSystemNotice(systemNotice);
		systemNotice = dao.save(systemNotice);
		for (String str : systemNotice.getAddressee()) {
			SystemNoticeAddressee addressee = new SystemNoticeAddressee();
			addressee.setName(str);
			addressee.setSystemNotice(systemNotice);
			systemNoticeAddresseeDao.save(addressee);
		}
		if (CommonUtil.isNotEmpty(systemNotice.getFileInfos())) {
			for (FileInfo fileInfo : systemNotice.getFileInfos()) {
				SystemNoticeAttachment attachment = new SystemNoticeAttachment();
				attachment.setFileInfo(fileInfo);
				attachment.setSystemNotice(systemNotice);
				systemNoticeAttachmentDao.save(attachment);
			}
		}
		return systemNotice;
	}

	/**
	 * 检查必填项
	 * 
	 * @param systemNotice
	 * @author: 超级小富翁 - BillDowney
	 * @date: 2019年3月28日 上午11:18:35
	 */
	private void checkSystemNotice(SystemNotice systemNotice) {
		CommonUtil.isNotEmpty(systemNotice.getSender(), new BizException(10601));
		CommonUtil.isNotEmpty(systemNotice.getSubject(), new BizException(10602));
		CommonUtil.isNotEmpty(systemNotice.getContent(), new BizException(10603));
		CommonUtil.isNotEmpty(systemNotice.getNoticeType(), new BizException(10604));
		CommonUtil.isNotEmpty(systemNotice.getAddressee(), new BizException(10605));
	}

}
